<template>
  <div class="aside">
    <div class="logoName" v-show="props.collapse == false" @click="toHome">
      <Logo>
        <template #name>
          <div class="login-aside">{{ tc("后台管理") }}</div>
        </template>
      </Logo>
    </div>
    <el-menu
      mode="vertical"
      background-color="#000"
      :collapse="props.collapse"
      text-color="#fff"
      class="el-menu-vertical-demo scrollbar_app"
      :default-openeds="defaultOpeneds"

      :unique-opened="false"
      :collapse-transition="false"
      :default-active="defaultActive"
      @open="handleOpen"
    >
      <div v-for="item in layoutData" :key="item.index">
        <el-sub-menu :index="item.index" v-if="item.child.length > 0">
          <template #title>
            <el-icon :color="'#fffff'" :size="18">
              <component :is="item.icon" />
            </el-icon>
            <span v-show="props.collapse == false">{{ item.name }}</span>
          </template>
          <el-menu-item-group v-if="item.child.length > 0">
            <el-menu-item
              v-for="itemChide in item.child"
              :index="itemChide.index"
              @click="headleMenuItem(itemChide)"
            >
              <el-icon>
                <component :is="itemChide.icon" />
              </el-icon>
              <template #title>
                <span>{{ itemChide.name }}</span>
              </template>
            </el-menu-item>
          </el-menu-item-group>
        </el-sub-menu>
        <el-menu-item
          :index="item.index"
          v-if="item.child.length == 0"
          @click="headleMenuItem(item)"
        >
          <el-icon :color="'#fffff'" :size="18">
            <component :is="item.icon" />
          </el-icon>
          <template #title>
            <span>{{ item.name }}</span>
          </template>
        </el-menu-item>
      </div>
    </el-menu>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from "vue";
import { layoutData } from "../layout/index";
import { useRouter, useRoute } from "vue-router";
import { tc } from "@/locales/index";
import Logo from "@/components/logo/index.vue";
// import {useChangeColor} from "@/utils/theme"
const router = useRouter();
const route = useRoute();
const defaultOpeneds = ref<any>([]);
const defaultActive = ref();

const props: any = defineProps({
  collapse: {
    type: Boolean, //当前屏幕减去侧边栏的宽度减去传入的宽度
    default: false,
  },
});
const handleOpen = () => {
  //key: string, keyPath: string[]
};
const headleMenuItem = (itemChide: any) => {
  defaultActive.value = itemChide.index;
  router.push(itemChide.routePath);
};
// 页面刷新对应上index
const activeFunInit = () => {
  // 当前的路由信息
  const routePath = route.fullPath;
  layoutData.forEach((element: any) => {
    // console.log(element.child)
    if (element.child.length > 0) {
      //如果有
      element.child.forEach((itemChide: any) => {
        if (itemChide.routePath == routePath) {
          defaultOpeneds.value = [element.index]; // 使用数组形式
          defaultActive.value = itemChide.index;
        }
      });
    } else {
      if (element.routePath == routePath) {
        defaultOpeneds.value = [];
        defaultActive.value = element.index;
      }
    }
  });
};
const toHome = () => {
  defaultOpeneds.value = ["0"]; // 使用数组形式
  router.push("/dashBoard");
};
watch(route, () => {
  activeFunInit();
});
onMounted(() => {
  activeFunInit();
});
</script>
<style lang="scss" scoped>
.el-menu-vertical-demo {
  height: calc(100vh - 60px);
  overflow-y: auto;
}
.aside {
  width: 100%;
  height: 100vh;
  background-color: #000;
  overflow: hidden;
}

.logoName {
  width: 100%;
  height: 55px;
  display: flex;
  align-items: center;
  font-weight: 900;
  margin: 0 auto;
  box-shadow: 0 2px 4px rgba(255, 255, 255, 0.1);
  cursor: pointer;
  margin-left: 20px;
}

.active {
  width: 8px;
  height: 8px;
  background-color: var(--el-color-primary);
  display: inline-block;
  border-radius: 50%;
  margin-right: 10px;
}

.activePrimary {
  width: 8px;
  height: 8px;
  display: inline-block;
  border-radius: 50%;
  margin-right: 10px;
}

::v-deep(.el-menu-item-group__title) {
  padding: 0px !important;
}

.el-menu-item-group .el-menu-item {
  width: 90%;
  height: 40px;
  line-height: 40px;
  margin: 0 auto;
  border-radius: 10px;
}
// .el-menu-item-group {
//   .is-active {
//     background-color: var(--el-color-primary) !important;
//     color: #fff !important;
//   }
// }


.el-menu-item:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

::v-deep(.el-sub-menu__title:hover) {
  background: rgba(255, 255, 255, 0.1) !important;
}

.el-menu {
  border: none !important;
  margin-top: 3px;
}

.imgLogo {
  width: 15px;
  height: 15px;
  display: inline-block;
  margin-right: 10px;
}
</style>
